/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package asphalt2.control
{
	import mx.core.IMXMLObject;
	import mx.events.FlexEvent;
	import mx.core.UIComponent;
	import asphalt2.validator.IValidateError;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	/**
	 * @author c9katayama
	 * @author shot
	 **/	
	public dynamic class AbstractControl extends EventDispatcher implements IControl, IMXMLObject
	{
		private var _ui:UIComponent
		
		public function get UI():UIComponent{
			return _ui
		}
		
		public function set UI(ui:UIComponent):void{
			this._ui = ui;
		}
		
		public function initialized(document:Object, id:String):void{
			UI = document as UIComponent;

			injectionProperty();

			var _preinit:Function = function(e:FlexEvent):void{
				UI.removeEventListener(FlexEvent.PREINITIALIZE,_preinit);
				preInit();
			};
			UI.addEventListener(FlexEvent.PREINITIALIZE,_preinit);

			var _postinit:Function = function(e:FlexEvent):void{
				UI.removeEventListener(FlexEvent.INITIALIZE,_postinit);
				postInit();
			};
			UI.addEventListener(FlexEvent.INITIALIZE,_postinit);

			var _setup:Function = function(e:FlexEvent):void{
				UI.removeEventListener(FlexEvent.CREATION_COMPLETE,_setup);
				setup();
			};
			UI.addEventListener(FlexEvent.CREATION_COMPLETE,_setup);
		}
		/**
		 * page という名前のプロパティがある場合、UIComponentをセットします。
		 */
		protected function injectionProperty():void{
			if(hasOwnProperty("page")){
				this.page = UI;
			}
		}
		/**このメソッドをオーバーライドすることで、UI初期化に処理を行えます。*/
		public function preInit():void
		{
		}
		/**このメソッドをオーバーライドすることで、UI初期化後に処理を行えます。*/
		public function postInit():void
		{
		}
		/**このメソッドをオーバーライドすることで、UI作成が全ておわったあとに初期化を行えます。*/
		public function setup():void
		{
		}
		/*
		public function validate():IValidateError
		{
			return null;
		}
		
		public function execute(e:Event):void
		{
			throw new Error("execute should be overrided.");
		}
		*/
	}
}